package handler

import (
	"day1110/cache"
	"day1110/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func MerHouseAdd(c *gin.Context) {
	name := c.PostForm("name")
	typ := c.PostForm("typ")
	status := c.PostForm("status")
	employee := c.PostForm("employee")
	if name == "" || typ == "" || status == "" || employee == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.MerHouse{
		Name:     name,
		Typ:      typ,
		Status:   status,
		Employee: employee,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "仓库信息添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	mid, _ := strconv.Atoi(c.PostForm("mid"))
	order_no, _ := strconv.Atoi(c.PostForm("order_no"))
	customer := c.PostForm("customer")
	if mid == 0 || order_no == 0 || customer == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Order{
		Mid:      mid,
		OrderNo:  order_no,
		Customer: customer,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "订单信息添加成功",
	})
	return
}
func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	var data []model.MerHouseOrder
	redisKey := "order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redisKey).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"mer_houses.name as Name",
				"mer_houses.typ as Typ",
				"mer_houses.status as Status",
				"mer_houses.employee as Employee",
				"orders.id as Oid",
				"orders.mid as Mid",
				"orders.order_no as OrderNo",
				"orders.customer as Customer").
			Joins("left join mer_houses on mer_houses.id = orders.mid").
			Limit(size).
			Offset(offset).
			Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redisKey, jsonData, time.Hour)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	var total int64
	model.DB.Table("orders").Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  data,
		"total": total,
	})
}
func OrderDetail(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	var data model.MerHouseOrder
	redisKey := "order_detail_" + strconv.Itoa(id)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redisKey).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"mer_houses.name as Name",
				"mer_houses.typ as Typ",
				"mer_houses.status as Status",
				"mer_houses.employee as Employee",
				"orders.id as Oid",
				"orders.mid as Mid",
				"orders.order_no as OrderNo",
				"orders.customer as Customer").
			Joins("left join mer_houses on mer_houses.id = orders.mid").
			Where("orders.id = ?", id).
			Limit(1).
			Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redisKey, jsonData, time.Hour)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
}
